<template>
  <div style="margin-top: 50px">
    <el-form ref="spuInfoForm" :model="value" :rules="rules" label-width="120px" style="width: 600px" size="small">
      <el-form-item label="商品分类：" prop="categoryId">
        <el-cascader v-model="selectedCategory" style="width: 100%" :options="options.categoryOptions" :props="{value: 'id', label: 'categoryName'}" placeholder="请选择商品分类" />
      </el-form-item>
      <el-form-item label="商品名称：" prop="spuName">
        <el-input v-model="value.spuName" />
      </el-form-item>
      <el-form-item label="标题：" prop="title">
        <el-input v-model="value.title" />
      </el-form-item>
      <el-form-item label="商品品牌：" prop="brandId">
        <el-select v-model="value.brandId" style="width: 100%" filterable clearable placeholder="请选择品牌" @change="handleBrandChange">
          <el-option v-for="option in options.brandOptions" :key="option.id" :label="option.brandName" :value="option.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="商品售价：">
        <el-input v-model="value.price" type="number" placeholder="请输入商品售价" />
      </el-form-item>
      <el-form-item label="商品批发价：">
        <el-input v-model="value.wholesalePrice" type="number" placeholder="请输入商品批发价" />
      </el-form-item>
      <el-form-item label="货号：">
        <el-input v-model="value.sn" />
      </el-form-item>
      <el-form-item label="型号：">
        <el-input v-model="value.model" />
      </el-form-item>
      <el-form-item label="商品库存：">
        <el-input v-model="value.stock" type="number" />
      </el-form-item>
      <el-form-item label="预警库存：">
        <el-input v-model="value.warningStock" type="number" />
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button type="primary" size="medium" @click="handleNext('spuInfoForm')">下一步，填写商品属性</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import CategoryApi from '@/api/goods/CategoryApi'
import BrandApi from '@/api/goods/BrandApi'

export default {
  name: 'SpuInfo',
  props: {
    value: {
      type: Object,
      default: function() {
        return {}
      }
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      options: {
        categoryOptions: [],
        brandOptions: []
      },
      isInitialized: false,
      selectedCategory: [], // 选中商品分类的值
      rules: {
        categoryId: [{ required: true, message: '请选择商品分类', trigger: 'blur' }],
        spuName: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' }
        ],
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        brandId: [{ required: true, message: '请选择品牌', trigger: 'blur' }]
      }
    }
  },
  watch: {
    value: function(newValue) {
      if (!this.isEdit) {
        return
      }
      if (this.isInitialized) {
        return
      }
      console.log(newValue)
      if (newValue.id) {
        this.init()
      }
    },
    selectedCategory(val, oldVal) {
      if (val != null) {
        this.value.categoryId = val[val.length - 1 > 0 ? val.length - 1 : 0]
      } else {
        this.value.categoryId = null
      }
    }
  },
  created() {
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      BrandApi.list().then(response => {
        this.options.brandOptions = response.data
      })
      CategoryApi.tree().then(response => {
        this.options.categoryOptions = this.$common.list2Tree(response.data)
      })
    },
    init() {
      // 初始化选中的分类
      this.selectedCategory.push(...this.value.categoryAncestorIds.split(','))
      this.selectedCategory.push(this.value.categoryId)
    },
    handleNext(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('nextStep')
        } else {
          this.$message({
            message: '验证失败',
            type: 'error',
            duration: 1000
          })
          return false
        }
      })
    },
    handleBrandChange(val) {
      this.options.brandOptions.forEach(option => {
        if (option.id === val) {
          this.value.brandName = option.brandName
          return
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
